﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using System.Web.UI.WebControls;

namespace RooHui.Admin.Web.Controllers
{
    [HandleError]
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页获取产品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetProductListByPage(int page, int pageSize, string key="")
        {
            int count = 0;
            var productList = ProductBiz.GetProductListByPage(page, pageSize, key, ref count);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("page", page);
            dic.Add("Rows", productList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult GetProductListByKey(string key = "")
        {
            var productList = ProductBiz.GetProductList(key);
            productList.Insert(0, new Product() { ID = -1, ProductName = "全部" });

            return Content(productList.ToJson());
        }

        /// <summary>
        /// 获取启用状态的产品
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetEnabledProduct() 
        {
            var productlist = ProductBiz.GetProductByStatus(1);
            productlist.Insert(0, new Product() { ID = -1, ProductName = "请选择" });
            return Content(productlist.ToJson());
        }

        /// <summary>
        /// 增加/修改产品
        /// </summary>
        /// <param name="productModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductSave(int editType, int productId, string productName, string summary, string rateSummary)
        {
            bool bSaveFlag = false;
            if (editType == 1)
            {
                bSaveFlag = ProductBiz.ProductAdd(productName, summary, rateSummary);
            }
            else if (editType == 2)
            {
                bSaveFlag = ProductBiz.ProductEdit(productId, productName, summary, rateSummary);
            }

            return Content(bSaveFlag.ToString());
        }

        [HttpPost]
        public ActionResult ProductStatusChange(int productId, int destinationStatus)
        {
            bool bChangeFlag = ProductBiz.ProductStatusChange(productId, destinationStatus);
            return Content(bChangeFlag.ToString());
        }
    }

}
